package com.yanqu.backendweb.validation.validators;


import com.yanqu.backendweb.config.ServerDbMgr;
import com.yanqu.backendweb.entity.TSDbInfo;
import com.yanqu.backendweb.entity.TSServerList;
import com.yanqu.backendweb.service.TSDbInfoService;
import com.yanqu.backendweb.utils.ThrowUtil;
import com.yanqu.backendweb.utils.eErrorCode;
import com.yanqu.backendweb.vo.GameServerValidVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author duli
 * @version 1.0
 * @date 2020/11/02 11:47
 * @Description 区服校验是否存在
 */
@Service
public class GameServerValid implements CustomizerValidService<Object> {
    @Autowired
    private TSDbInfoService tsDbInfoService;

    /**
     * 这里如果抛出 }
     * 会被 {@link javax.validation.ValidationException} 捕捉
     * ExceptionIntercept.systemExceptionHandler 做了相应处理
     *
     */
    @Override
    public boolean isValid(Object value) {
        if (value instanceof GameServerValidVo) {
            GameServerValidVo vo = (GameServerValidVo) value;
            validPL(vo.getServer_id());

        }
        return true;
    }

    private void validPL(Integer serverId) {
        if(serverId==null){
            ThrowUtil.businessFault(eErrorCode.SERVER_NULL);
        }
        TSDbInfo dbInfo = ServerDbMgr.getDbInfoByServerId(serverId);
        TSServerList serverList = ServerDbMgr.getServerByServerId(serverId);
        if(dbInfo==null||serverList==null){
            tsDbInfoService.loadDbToMemory();
            dbInfo = ServerDbMgr.getDbInfoByServerId(serverId);
            serverList = ServerDbMgr.getServerByServerId(serverId);
        }
        if(dbInfo==null||serverList==null){
            ThrowUtil.businessFault(eErrorCode.SERVER_NOT_EXISTS);
        }
    }
}
